package com.es.www.kyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.es.www.kyl.bean.req.client.UpdateUserReq;
import com.es.www.kyl.bean.req.web.UserReq;
import com.es.www.kyl.bean.req.web.UserUpdateReq;
import com.es.www.kyl.bean.res.web.UserVO;
import com.es.www.kyl.domain.User;
import com.es.www.kyl.mapper.UserMapper;
import com.es.www.kyl.service.UserService;
import com.es.www.kyl.util.UserContextHolder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 * @description 针对表【user_info】的数据库操作Service实现
 * @createDate 2023-04-01 14:12:08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


	private final UserMapper userMapper;

	public UserServiceImpl(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Override
	public PageInfo<UserVO> pageQueryUserList(UserReq userReq) {
		PageHelper.startPage(userReq.getPageNum(), userReq.getPageSize());
		return new PageInfo<>(userMapper.selectUserList(userReq));
	}

	@Override
	public void updateUser(UserUpdateReq userUpdateReq) {
		User user = new User();
		user.setId(userUpdateReq.getUserId());
		user.setName(userUpdateReq.getUserName());
		user.setUserRole(userUpdateReq.getUserRole());
		user.setPerType(userUpdateReq.getPerType());
		user.setUpdateBy(UserContextHolder.getUserId());
		user.setUpdateTime(new Date());
		userMapper.updateById(user);
	}

	@Override
	public UserVO getUserInfo() {
		Integer userId = UserContextHolder.getUserId();
		User user = userMapper.selectById(userId);
		if (user != null) {
			UserVO userVo = new UserVO();
			userVo.setId(user.getId());
			userVo.setName(user.getName());
			userVo.setUserRole(user.getUserRole());
			userVo.setUserPhone(user.getUserPhone());
			userVo.setPerType(user.getPerType());
			return userVo;
		}
		return null;
	}

	@Override
	public void updateUserInfo(UpdateUserReq req) {
		Integer userId = UserContextHolder.getUserId();
		User user = new User();
		user.setId(userId);
		user.setName(req.getName());
		user.setAvatarUrl(req.getAvatarUrl());
		user.setUserPhone(req.getUserPhone());
		user.setUpdateBy(userId);
		user.setUpdateTime(new Date());
		userMapper.updateById(user);
	}
}




